package 面试题04.从右上角标志位开始检索;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 11:13 2021/2/3
 * @Description：
 */
public class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {

        // 必须要有特殊输入测试！错误检测！
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }

        // 从右上角开始检索
        int column = matrix[0].length - 1;
        int row = 0;

        while (row < matrix.length && column >= 0) {
            if (matrix[row][column] == target) {
                return true;
            } else if (target > matrix[row][column]) {
                row++;
            } else {
                column--;
            }
        }

        return false;
    }
}
